// pages/publishcomment/publishcomment.js

//获取应用实例
const app = getApp();
var count_i = 0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    flag2: 5,
    iszan:false,
    have_content:false,
    textvalue:'',
    evalList: [{ tempFilePaths: [], imgList: [] }],
    add_hidden:false

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  
  },
  changeColor:function(e){
    var that = this;
    
    console.log(e.target.dataset.index);

    var i = e.target.dataset.index+1;

    that.setData({
      flag2: i
    });
  },
 
  onChange:function(e){
    var value = e.detail.value;
    if (value.length > 0) {
      this.setData({
        have_content: true,
      });
    }
  },
  //评价内容
  blurValue:function(e){
    console.log(e.detail.value);
    if (e.detail.value.length>0){
      this.setData({
        have_content:true
      })  
    }
    
  },
  gozan:function(){
    this.setData({
      iszan: !this.data.iszan
    });
  },
  //添加图片
  joinPicture: function (e) {
   
    count_i++;
    if (count_i >= 3){
     this.setData({
       add_hidden: true
     })
    }  

    var index = e.currentTarget.dataset.index;

    var evalList = this.data.evalList;

    var that = this;

    var imgNumber = evalList[index].tempFilePaths;

    if (imgNumber.length >= 3 ) {

      wx.showModal({

        title: '',

        content: '最多上传三张图片',

        showCancel: false,

      })

      return;

    }

    wx.showActionSheet({

      itemList: ["从相册中选择", "拍照"],

      itemColor: "#f7982a",

      success: function (res) {

        if (!res.cancel) {

          if (res.tapIndex == 0) {

            that.chooseWxImage("album", imgNumber);

          } else if (res.tapIndex == 1) {

            that.chooseWxImage("camera", imgNumber);

          }

        }

      }

    })

  },

  chooseWxImage: function (type, list) {

    var img = list;

    var len = img.length;

    var that = this;

    var evalList = this.data.evalList;

    wx.chooseImage({

      count: 4 - count_i,

      sizeType: ["original", "compressed"],

      sourceType: [type],

      success: function (res) {

        var addImg = res.tempFilePaths;

        var addLen = addImg.length;
        if (addLen >= 3 || evalList.length + addLen >=3){
          count_i = 3;
          that.setData({
            add_hidden: true
          })
        }else{
          count_i = addLen;
          that.setData({
            add_hidden: false
          })
        }


        if ((len + addLen) > 3) {
         
          for (var i = 0; i < (addLen - len); i++) {

            var str = {};

            str.pic = addImg[i];

            img.push(str);

          }

        } else {

          for (var j = 0; j < addLen; j++) {

            var str = {};

            str.pic = addImg[j];

            img.push(str);

          }

        }

        that.setData({

          evalList: evalList

        })

        //上传图片
        // console.log(img,'img');
        // that.upLoadImg(img);


      },

    })

  },

  upLoadImg: function (list) {

    var that = this;

    this.upload(that, list[0].tempFilePaths);

  },
  //多张图片上传

  upload: function (page, path) {

    var that = this;

    var curImgList = [];

    for (var i = 0; i < path.length; i++) {

      wx.showToast({

        icon: "loading",

        title: "正在上传"

      }),

        wx.uploadFile({

          url: app.apiList.upload_img + '',//接口处理在下面有写

          filePath: path[i].pic,

          name: 'file',

          header: { "Content-Type": "multipart/form-data" },

          formData: {

            douploadpic: '1'

          },

          success: function (res) {

            curImgList.push(res.data);

            var evalList = that.data.evalList;

            evalList[0].imgList = curImgList;

            that.setData({

              evalList: evalList

            })

            if (res.statusCode != 200) {

              wx.showModal({

                title: '提示',

                content: '上传失败',

                showCancel: false

              })

              return;

            }

            var data = res.data

            page.setData({  //上传成功修改显示头像

              src: path[0]

            })

          },

          fail: function (e) {

            wx.showModal({

              title: '提示',

              content: '上传失败',

              showCancel: false

            })

          },

          complete: function () {

            wx.hideToast();  //隐藏Toast

          }

        })

    }

  },
  //删除图片

  clearImg: function (e) {

    count_i--;
    if (count_i<3){
      this.setData({
        add_hidden: false
      })
    }
    var index = e.currentTarget.dataset.index;

    var evalList = this.data.evalList;

    var img = evalList[0].tempFilePaths;

    img.splice(index, 1);

    this.setData({

      evalList: evalList

    })

    this.upLoadImg(img);

  },
  pushcomment_pics:function(){
    var that = this;

    console.log(that.data.evalList,'that.data.evalList');

    //上传图片
    that.upLoadImg(that.data.evalList);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})

// https://blog.csdn.net/weixin_38668828/article/details/79272455
// http://www.cnblogs.com/xjwy/p/6956120.html